/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TUILayer;

import DomainLayer.*;
import java.util.Scanner;
import utils.*;

/**
 *
 * @author Middreks
 * Licensed under GNU/GPLv2
 * Credits go to Group 4
 */
public class AddressBookUI {
    private AddressBook adrBook;
    private IdGenerator idGen;
    private int selection = -1;
    private Scanner input = new Scanner(System.in);
    private String string = "";
    //initializing for person
    private String name = "";
    private String address = "";
    private int postalCode = -1;
    private String city = "";
    private int phone = -1;
    private int id = 0;
    private Person p = new Person(id, name, address, postalCode, city, phone);
    private String field = "";
    private String newStringValue = "";
    private int newIntValue = 0;

    public AddressBookUI(AddressBook addressBook) {
        adrBook = addressBook;
        idGen = new IdGenerator();
    }

    /**
     * Menu of the Address Book
     */
    public void addressBookMenu() {

        while (selection != 0) {
            System.out.print("#########################\n" +
                    "DVDBook Rental System 0.2\n" +
                    "Address Book - Customer Management\n" +
                    "#########################\n" +
                    "Please choose from the following\n" +
                    "\t*1* Add person\n" +
                    "\t*2* Remove Person by ID\n" +
                    "\t*3* Edit Person (String field)\n" +
                    "\t*4* Edit Person (int field)\n" +
                    "\t*5* Print out Address Book\n" +
                    "\n" +
                    "\t*0* Exit to Main Menu\n");

            if (input.hasNextInt()) {
                selection = input.nextInt();
                switch (selection) {
                    case 0:
                        break;
                    case 1:
                        ConsoleUtils.clear();
                        addPerson();
                        break;
                    case 2:
                        ConsoleUtils.clear();
                        removePerson();
                        break;
                    case 3:
                        ConsoleUtils.clear();
                        editPersonString();
                        break;
                    case 4:
                        ConsoleUtils.clear();
                        editPersonInt();
                        break;
                    case 5:
                        ConsoleUtils.clear();
                        printPeople();
                        break;
                    default:
                        System.out.println("Incorrect input!");
                }
            } else {
                string = input.next();
                ConsoleUtils.clear();
                System.out.printf("%s Not a number!\n", string);
            }

        }

        selection = -1;
        ConsoleUtils.clear();
    }

    private void addPerson() {
        System.out.println("Type the name of the Person:");
        if (input.hasNext()) {
            name = input.next();
        }
        System.out.println("Type the street address:");
        if (input.hasNext()) {
            address = input.next();
        }
        System.out.println("Type the postal code:");
        do {
            if (input.hasNextInt()) {
                postalCode = input.nextInt();
            } else {
                System.out.println("Error: number must be entered!");
                input.next();
            }
        } while (postalCode == -1);
        System.out.println("Type the city:");
        if (input.hasNext()) {
            city = input.next();
        }
        do {
            System.out.println("Type the phone number (int):");

            if (input.hasNextInt()) {
                phone = input.nextInt();
            } else {
                System.out.println("Error: number must be entered!");
                input.next();
            }
        } while (phone == -1);
        id = idGen.getNextId();
        p = new Person(id, name, address, postalCode, city, phone);
        adrBook.addPerson(p);

        ConsoleUtils.clear();
        System.out.println("Person was successfully added.");
    }

    private void removePerson() {
        System.out.println("Type the exact Id of the person" +
                " you would like to delete (int):");
        if (input.hasNextInt()) {
            id = input.nextInt();
        }
        if (adrBook.getPersonById(id) != null) {
            adrBook.removePerson(id);
        } else {
            ConsoleUtils.clear();
            System.out.println("Person with the specified id doesn't exist.");
            return;
        }

        ConsoleUtils.clear();
    }

    private void editPersonString() {
        System.out.print("Please type in the ID of the " +
                "Person, the field you want to edit, and " +
                "the new value for it\nPossible fields are " +
                "'name', 'address' and 'city'.\n");
        //asking for ID
        askForID();

        //Asking for field
        askForField();

        //Asking for the new value
        askForNewStringField();

        if (!adrBook.editPersonStringById(id, field, newStringValue))
        {
            ConsoleUtils.clear();
            System.out.println("Incorrect field value entered! Use 'name', " +
                    "'address' or 'city'!");
            return;
        }
        ConsoleUtils.clear();
    }

    private void editPersonInt() {
        System.out.print("Please type in the ID of the Person," +
                "the field you want to edit, and the new value" +
                " for it\nPossible fields are 'postalcode' and" +
                " 'phone'.\n");
        //asking for ID
        askForID();
        //Asking for field
        askForField();

        //Asking for the new value
        askForNewIntField();

        if (!adrBook.editPersonIntById(id, field, newIntValue))
        {
            ConsoleUtils.clear();
            System.out.println("Incorrect field value entered! Use " +
                    "'postalcode' or 'phone'!");
            return;
        }
        ConsoleUtils.clear();
    }

    private void printPeople() {
        if (adrBook.getPeople().length != 0) {
            Printer.print(adrBook.getPeople());
        } else {
            System.out.println("The database is empty, fill it up first!");
        }
    }

    private void askForID() {
        System.out.println("\nID:\n");
        if (input.hasNextInt()) {
            id = input.nextInt();
        }
    }

    private void askForField() {
        System.out.println("\nField:\n");
        if (input.hasNext()) {
            field = input.next();
        }
    }

    private void askForNewStringField() {
        System.out.println("Enter the new value for the field:");
        if (input.hasNext()) {
            newStringValue = input.next();
        }
    }

    private void askForNewIntField() {
        System.out.println("Enter the new value for the field:");
        if (input.hasNextInt()) {
            newIntValue = input.nextInt();
        }
    }
}
